package com.dc.bobaoge.project.v2.to.v3.business.core.util;

import com.dc.bobaoge.base.entity.core.po.AuthenticationInfo;
import com.dc.bobaoge.base.entity.core.po.AuthenticationInfoHistory;
import com.dc.bobaoge.base.mapper.core.AuthenticationInfoHistoryMapper;
import com.dc.bobaoge.base.mapper.core.AuthenticationInfoMapper;
import com.dc.bobaoge.common.constant.AuthenticationInfoTypeEnum;
import com.dc.bobaoge.common.em.PersonalCertificateEnum;
import com.dc.bobaoge.project.base.core.constant.AuthInfoConstant;
import com.dc.bobaoge.project.base.core.old.entity.OldCoreCerMessage;
import com.dc.bobaoge.project.base.core.old.entity.OldCoreCerPerson;
import com.own.business.sync.trigger.base.entity.SyncTrigger;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * CerPersonSyncUtil
 *
 * @author chenxueli
 * @date 2024-01-29 10:06:00
 */
@SuppressWarnings("all")
@Component
public class CerPersonSyncUtil {

    @Resource
    private AuthenticationInfoMapper authenticationInfoMapper;
    @Resource
    private AuthenticationInfoHistoryMapper authenticationInfoHistoryMapper;

    @Resource
    private CerCommonUtil cerCommonUtil;

    /**
     * 完善信息
     *
     * @param trigger           日志信息
     * @param oldCoreCerMessage 新的认证信息
     * @param person            新的认证人信息
     */
    public void runInfoPass(SyncTrigger trigger, OldCoreCerMessage oldCoreCerMessage, OldCoreCerPerson person) {
        // 查询是否已经存在信息了
        var history = authenticationInfoHistoryMapper.selectById(oldCoreCerMessage.getCerMsgId());
        if (history == null) {
            history = new AuthenticationInfoHistory();
            history.setAuditStatus(CerCommonUtil.STATUS_MAP.get(oldCoreCerMessage.getCerMsgState()));
            history.setId(oldCoreCerMessage.getCerMsgId());
            history.setVipType(oldCoreCerMessage.getCerType());
            history.setAdvertisementType(0);
            history.setFkUserId(oldCoreCerMessage.getCerUserId());
            history.setFkVipId(oldCoreCerMessage.getCerLevelId());
            history.setRfVipName(oldCoreCerMessage.getCerLevelName());
            history.setAuditRemark(oldCoreCerMessage.getCerMsgReason());
            history.setAuditTime(AuthInfoConstant.containsOldStatusAudit(oldCoreCerMessage.getCerMsgState()) ? oldCoreCerMessage.getCerMsgVerfiy() : null);
            history.setAvatar(person.getCerPersonHeadimg());
            history.setAddress(person.getCerPersonAddress());
            history.setDetailAddress(person.getCerPersonAddress());
            history.setCertificateFront(person.getCerPersonImg());
            history.setCertificateBack(person.getCerPersonCon());
            history.setCertificateHand(person.getCerPersonHand());
            history.setCertificateType(PersonalCertificateEnum.RECOGNIZE_ID_CARD.getType());
            history.setCertificateNo(person.getCerPersonIdcard());
            history.setName(person.getCerPersonName());
            history.setNickname(person.getCerUserNickname());
            history.setPhone(person.getCerPersonPhone());
            history.setRfBusinessTypeName(cerCommonUtil.resolveBusinessTypeName(person.getCerPersonRange()));
            history.setFkBusinessTypeId(cerCommonUtil.getBusinessTypeIdByName(history.getRfBusinessTypeName()));
            history.setOtherLicense("");
            history.setBankCardNo(person.getCerPersonBankcard());
            history.setBusinessLicense("");
            authenticationInfoHistoryMapper.insert(history);
        } else {
            history.setAuditStatus(CerCommonUtil.STATUS_MAP.get(oldCoreCerMessage.getCerMsgState()));
            history.setId(oldCoreCerMessage.getCerMsgId());
            history.setVipType(oldCoreCerMessage.getCerType());
            history.setFkUserId(oldCoreCerMessage.getCerUserId());
            history.setFkVipId(oldCoreCerMessage.getCerLevelId());
            history.setRfVipName(oldCoreCerMessage.getCerLevelName());
            history.setAuditRemark(oldCoreCerMessage.getCerMsgReason());
            history.setAuditTime(AuthInfoConstant.containsOldStatusAudit(oldCoreCerMessage.getCerMsgState()) ? oldCoreCerMessage.getCerMsgVerfiy() : null);
            history.setAvatar(person.getCerPersonHeadimg());
            history.setAddress(person.getCerPersonAddress());
            history.setDetailAddress(person.getCerPersonAddress());
            history.setCertificateFront(person.getCerPersonImg());
            history.setCertificateBack(person.getCerPersonCon());
            history.setCertificateHand(person.getCerPersonHand());
            history.setCertificateType(PersonalCertificateEnum.RECOGNIZE_ID_CARD.getType());
            history.setCertificateNo(person.getCerPersonIdcard());
            history.setName(person.getCerPersonName());
            history.setNickname(person.getCerUserNickname());
            history.setPhone(person.getCerPersonPhone());
            history.setRfBusinessTypeName(cerCommonUtil.resolveBusinessTypeName(person.getCerPersonRange()));
            history.setFkBusinessTypeId(cerCommonUtil.getBusinessTypeIdByName(history.getRfBusinessTypeName()));
            history.setBankCardNo(person.getCerPersonBankcard());
            authenticationInfoHistoryMapper.updateById(history);
        }
        // 查询押金信息
        var cerPayDeposit = cerCommonUtil.getOldCoreCerPayDepositByMsgId(oldCoreCerMessage.getCerUserId());
        // 查询对应的信息
        var authInfo = cerCommonUtil.getAuthInfoByUserId(oldCoreCerMessage.getCerUserId());
        if (authInfo == null) {
            authInfo = new AuthenticationInfo();
            authInfo.setFkUserId(history.getFkUserId());
            authInfo.setFkVipId(history.getFkVipId());
            authInfo.setRfVipName(history.getRfVipName());
            authInfo.setVipType(history.getVipType());
            authInfo.setAdvertisementType(history.getVipType());
            authInfo.setIsLegal(history.getIsLegal());
            authInfo.setAvatar(history.getAvatar());
            authInfo.setAddress(history.getAddress());
            authInfo.setDetailAddress(history.getDetailAddress());
            authInfo.setCertificateFront(history.getCertificateFront());
            authInfo.setCertificateBack(history.getCertificateBack());
            authInfo.setCertificateHand(history.getCertificateHand());
            authInfo.setCertificateType(history.getCertificateType());
            authInfo.setCertificateNo(history.getCertificateNo());
            authInfo.setName(history.getName());
            authInfo.setNickname(history.getNickname());
            authInfo.setPhone(history.getPhone());
            authInfo.setRfBusinessTypeName(cerCommonUtil.resolveBusinessTypeName(history.getRfBusinessTypeName()));
            authInfo.setFkBusinessTypeId(cerCommonUtil.getBusinessTypeIdByName(authInfo.getRfBusinessTypeName()));
            authInfo.setOtherLicense(history.getOtherLicense());
            authInfo.setBankCardNo(history.getBankCardNo());
            authInfo.setBusinessLicense(history.getBusinessLicense());
            authInfo.setLegalCertificateFront(history.getLegalCertificateFront());
            authInfo.setLegalCertificateBack(history.getLegalCertificateBack());
            authInfo.setLegalCertificateHand(history.getLegalCertificateHand());
            authInfo.setLegalCertificateNo(history.getLegalCertificateNo());
            authInfo.setLegalName(history.getLegalName());
            authInfo.setLegalPhone(history.getLegalPhone());
            authInfo.setLegalCertificateType(history.getLegalCertificateType());
            authInfo.setDays(0);
            authInfo.setFirstTime(history.getAuditTime());
            authInfo.setStartTime(history.getAuditTime());
            authInfo.setEndTime(history.getAuditTime().plusYears(1));
            authInfo.setTypeText(AuthenticationInfoTypeEnum.PERSONAL.getType());
            if (cerPayDeposit != null) {
                authInfo.setDeposit((int) (cerPayDeposit.getCerPayTotal() * 100));
                authInfo.setDepositTradeNo(cerPayDeposit.getCerOrderNo());
            }
            authenticationInfoMapper.insert(authInfo);
        } else {
            authInfo.setFkUserId(history.getFkUserId());
            authInfo.setFkVipId(history.getFkVipId());
            authInfo.setRfVipName(history.getRfVipName());
            authInfo.setVipType(history.getVipType());
            authInfo.setAdvertisementType(history.getVipType());
            authInfo.setIsLegal(history.getIsLegal());
            authInfo.setAvatar(history.getAvatar());
            authInfo.setAddress(history.getAddress());
            authInfo.setDetailAddress(history.getDetailAddress());
            authInfo.setCertificateFront(history.getCertificateFront());
            authInfo.setCertificateBack(history.getCertificateBack());
            authInfo.setCertificateHand(history.getCertificateHand());
            authInfo.setCertificateType(history.getCertificateType());
            authInfo.setCertificateNo(history.getCertificateNo());
            authInfo.setName(history.getName());
            authInfo.setNickname(history.getNickname());
            authInfo.setPhone(history.getPhone());
            authInfo.setRfBusinessTypeName(cerCommonUtil.resolveBusinessTypeName(history.getRfBusinessTypeName()));
            authInfo.setFkBusinessTypeId(cerCommonUtil.getBusinessTypeIdByName(authInfo.getRfBusinessTypeName()));
            authInfo.setOtherLicense(history.getOtherLicense());
            authInfo.setBankCardNo(history.getBankCardNo());
            authInfo.setBusinessLicense(history.getBusinessLicense());
            authInfo.setLegalCertificateFront(history.getLegalCertificateFront());
            authInfo.setLegalCertificateBack(history.getLegalCertificateBack());
            authInfo.setLegalCertificateHand(history.getLegalCertificateHand());
            authInfo.setLegalCertificateNo(history.getLegalCertificateNo());
            authInfo.setLegalName(history.getLegalName());
            authInfo.setLegalPhone(history.getLegalPhone());
            authInfo.setLegalCertificateType(history.getLegalCertificateType());
            authInfo.setTypeText(AuthenticationInfoTypeEnum.PERSONAL.getType());
            if (cerPayDeposit != null) {
                authInfo.setDeposit((int) (cerPayDeposit.getCerPayTotal() * 100));
                authInfo.setDepositTradeNo(cerPayDeposit.getCerOrderNo());
            }
            authenticationInfoMapper.updateById(authInfo);
        }
    }

}
